import requests
import json
def base64_image(img_path):
    '''\
    img_path :图片连接
    return :  返回图片的base64编译字符串
    '''
    import base64
    with open(img_path,'rb') as f:
        base64_data = base64.b64encode(f.read())

    return str(base64_data,'utf-8')


class ReturnAiFaceUrl:
    detect_url_cla = 'https://aip.baidubce.com/rest/2.0/face/v3/detect?\
access_token={}'#提交检测图片的链接
    match_url_cla = 'https://aip.baidubce.com/rest/2.0/face/v3/match?\
access_token={}'#提交对比图片的链接
    host_cla = 'https://aip.baidubce.com/oauth/2.0/\
token?grant_type=client_credentials&client_id=\
{}&client_secret={}'#获取access_token值的链接
    add_to_ku_cla = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add?\
access_token={}"#人脸添加到库中
    get_user_cla ="https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/get?\
access_token={}"#人脸库中查询"
    get_group_cla = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/getusers?\
access_token={}"#人脸库中查询组的用户
    get_group_id_cla = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/getlist?\
access_token={}"#人脸库中的的组名查询
    search_url_cla = "https://aip.baidubce.com/rest/2.0/face/v3/search?\
access_token={}"#从库中检查"
    header = {'Content-Type': 'application/json; charset=UTF-8'} 
    
    def __init__(self,
                 ak = 'ARAhQ54l9eTGmUhszx8iPZAF',
                 sk = 'aG5urwxj3U1FWidG1BIFWp79oyNVEky7',
                 ):
        self.ak = ak
        self.sk = sk
        self.host = self.host_cla.format(ak,sk)
        
        self.access_token = requests.get(
            self.host,
            headers = self.header).json()['access_token']
        
        self.detect_url = self.detect_url_cla.format(
            self.access_token)
        
        self.match_url = self.match_url_cla.format(
            self.access_token)

        self.add_to_ku_url = self.add_to_ku_cla.format(
            self.access_token)

        self.get_user_url = self.get_user_cla.format(
            self.access_token)
        self.get_group_url = self.get_group_cla.format(
            self.access_token)
        self.get_group_id_url = self.get_group_id_cla.format(
            self.access_token)
        self.search_url = self.search_url_cla.format(
            self.access_token)
    
if __name__ == '__main__':
    import json
    x = ReturnAiFaceUrl()
    url = x.search_url
    image = base64_image(
        'C:/Users/Administrator/Desktop/新建文件夹/img/微信图片_20180724221115.jpg')
    info_dict = {"image":image,
                 "image_type":"BASE64",
                 "group_id_list":"group_repeat,group_233",
                 "quality_control":"LOW",
                 "liveness_control":"NORMAL"}
    header = {'Content-Type':'application/json'}
    response = requests.post(url,
                             headers = header,
                             data = json.dumps(info_dict),
                             )
    return_info = response.json()
    print(return_info)



    
'''    
    x = ReturnAiFaceUrl()
    url = x.get_group_id_url
    info_dict = {"start":0,"length":100}
    header = {'Content-Type':'application/json'}
    response = requests.post(url,
                             headers = header,
                             data = json.dumps(info_dict),
                             )
    return_info = response.json()
    print(return_info)
'''    
    
'''
    x = ReturnAiFaceUrl()
    url = x.get_group_url
    info_dict = {"group_id":"group_repeat"}
    header = {'Content-Type':'application/json'}
    response = requests.post(url,
                             headers = header,
                             data = json.dumps(info_dict),
                             )
    return_info = response.json()
    print(return_info)
'''    
'''

    x = ReturnAiFaceUrl()
    get_url = x.get_user_url
    info_dict = {"user_id":"user1","group_id":"@ALL"}
    header = {'Content-Type':'application/json'}
    response = requests.post(get_url,
                             headers = header,
                             data = json.dumps(info_dict),
                             )
    return_info = response.json()
    print(return_info)
'''
    
'''    
    x = ReturnAiFaceUrl()
    add_url = x.add_to_ku_url
    image = base64_image(
        'C:/Users/Administrator/Desktop/20180720181429.jpg')

    info_dict = {"image":image,
                 "image_type":"BASE64",
                 "group_id":"group_repeat",
                 "user_id":"user1",
                 "user_info":"abc",
                 "quality_control":"LOW",
                 "liveness_control":"NORMAL"}
    header = {'Content-Type':'application/json'}
    response = requests.post(add_url,
                             headers = header,
                             data = json.dumps(info_dict),
                             )
    return_info = response.json()
    print(return_info)
'''    

        
        





















